### Figure A7. ###

# Load packages 
library(readstata13)
library(ggplot2)
library(dplyr)
library(reshape2)

# Read Replication data 
data <- read.dta13("~/Dropbox/Replication_MVC/Datasets/datasets_analysis/panel_excombatientes.dta", 
                   convert.factors = TRUE, generate.factors = FALSE,
                   encoding = "UTF-8", fromEncoding = NULL, convert.underscore = FALSE,
                   missing.type = FALSE, convert.dates = TRUE, replace.strl = TRUE,
                   add.rownames = FALSE, nonint.factors = TRUE, select.rows = NULL)

# Prep type of data
data$Type_of_Crime2 <- stringi::stri_trans_general(data$Type_of_Crime, "nfd")
data$Type_of_Crime2 <- stringi::stri_trans_general(data$Type_of_Crime2, "ascii")

# Collapse (sum) captures by Type_of_Crime2
collapsed_data <- aggregate(diffcaptures ~ Type_of_Crime2, data = data, FUN = sum)

# Drop rows where Type_of_Crime2 is missing (".")
collapsed_data <- collapsed_data[!collapsed_data$Type_of_Crime2 == ".", ]

# Calculate sum of captures
sum_diffcaptures <- sum(collapsed_data$diffcaptures)

# Generate percent variable
collapsed_data$per2 <- collapsed_data$diffcaptures / sum_diffcaptures
collapsed_data$per2 <- collapsed_data$per2 * 100

# National level data
national_data <- read.dta13("~/Dropbox/Replication_MVC/Datasets/Muninicpality_year/crime_national.dta", 
                            convert.factors = TRUE, generate.factors = FALSE,
                            encoding = "UTF-8", fromEncoding = NULL, convert.underscore = FALSE,
                            missing.type = FALSE, convert.dates = TRUE, replace.strl = TRUE,
                            add.rownames = FALSE, nonint.factors = FALSE, select.rows = NULL)

#merge data
merged_data <- inner_join(collapsed_data, national_data, by = "Type_of_Crime2")

# Change some labels
merged_data <- merged_data %>%
  mutate(Type_of_Crime2 = ifelse(Type_of_Crime2 == "Illegal possession and trafficking of Military arms and ammunition",
                                 "Illegal use of military weapons", Type_of_Crime2))

merged_data <- merged_data %>%
  mutate(Type_of_Crime2 = ifelse(Type_of_Crime2 == "Illegal arms possession, trafficking or production",
                                 "Illegal use of weapons", Type_of_Crime2))


d1 <- data.frame(merged_data$per2, merged_data$National_Level, merged_data$Type_of_Crime2)
d2 <- melt(d1, id.vars='merged_data.Type_of_Crime2')

g2 <- ggplot(d2, aes(x=reorder(merged_data.Type_of_Crime2, value), y=value, fill=variable)) +
  geom_bar(position="dodge", stat="identity", width=0.7) +
  coord_flip() +
  xlab("") +
  ylab(label = "Percentage of captures (2013-2016)") +
  scale_y_continuous(expand = c(0, 0)) +
  theme(legend.title=element_blank(),
        legend.position = "bottom",
        legend.text = element_text(size = 7),
        axis.text.y = element_text(size = 7),
        axis.text.x = element_text(size = 7),
        axis.title.x = element_text(size = 8)) +
  scale_fill_grey(labels=c("Ex-combatants", "All Country"))

print(g2)


